
***************************************************************************************************************
/*
THIS DO-FILE:
Gets data moments for estimation and fit
*/
***************************************************************************************************************

use "my_sample_long.dta", clear
merge 1:1 hhidpn wave using "imputed_kid_assets.dta", nogen
rename assets passets

sum passets if (cohort==1 & wave==10) | (cohort==2 & wave==8) | (cohort==3 & wave==6) | (cohort==4 & wave==4), det

*justification for no insurance decision for people initially in bad health
tab sick if cohort==1 & wave==4, sum(ltci) 

*medicaid
reg medicaid
lincom _cons
disp r(se)^(-2)

*kid labor force participation rate
reg kidworkFT
lincom _cons
disp r(se)^(-2)

table careneed, stat(mean kidworkNT kidworkPT kidworkFT)
reg kidworkFT i.careneed, baselevels
lincom _cons+1.careneed
disp r(se)^(-2)
lincom _cons+2.careneed
disp r(se)^(-2)
lincom _cons+3.careneed
disp r(se)^(-2)

sum kidworkFT kidworkPT if careneed==1
sum kidworkFT kidworkPT if inlist(careneed,2,3)

tab cohort, sum(kidworkFT)
reg kidworkFT i.cohort, baselevels
lincom _cons+1.cohort
disp r(se)^(-2)
lincom _cons+2.cohort
disp r(se)^(-2)
lincom _cons+3.cohort
disp r(se)^(-2)
lincom _cons+4.cohort
disp r(se)^(-2)
lincom _cons+5.cohort
disp r(se)^(-2)

reg informaldummy if inlist(careneed,2,3)
disp _se[_cons]^(-2)
reg ltci
disp _se[_cons]^(-2)
reg medicaid
disp _se[_cons]^(-2)

*"bequest" -- this is assets in the year before died
sort hhidpn wave
by hhidpn: gen firstdeath=1 if inw==0 & inw[_n-1]==1
by hhidpn: gen bequest=passets if inw==1 & firstdeath[_n+1]==1
sum bequest, det
sum bequest if cohort<6, det
preserve
pctile ptile_amount_data=bequest, nq(1000)
gen ptile=_n
twoway scatter ptile ptile_amount_data if ptile_amount_data<1000000
keep ptile ptile_amount_data
save "bequest_percentiles_data.dta", replace
restore

xtile apq=passets, nq(5) 

tab apq, sum(ltci)
reg ltci i.apq, baselevels
lincom _cons+1.apq
disp r(se)^(-2)
lincom _cons+2.apq
disp r(se)^(-2)
lincom _cons+3.apq
disp r(se)^(-2)
lincom _cons+4.apq
disp r(se)^(-2)
lincom _cons+5.apq
disp r(se)^(-2)

tab apq if inlist(careneed,2,3), sum(informaldummy)
reg informaldummy i.apq if inlist(careneed,2,3), baselevels
lincom _cons+1.apq
disp r(se)^(-2)
lincom _cons+2.apq
disp r(se)^(-2)
lincom _cons+3.apq
disp r(se)^(-2)
lincom _cons+4.apq
disp r(se)^(-2)
lincom _cons+5.apq
disp r(se)^(-2)

tab apq, sum(kidworkFT)
tab apq, sum(kidworkPT)

tab apq, sum(medicaid)

tab apq if inlist(careneed,2,3) & inrange(age,65,74), sum(informaldummy)
tab apq if inlist(careneed,2,3) & inrange(age,75,84), sum(informaldummy)
tab apq if inlist(careneed,2,3) & inrange(age,85,94), sum(informaldummy)

*"nursing home" rates
gen formalcare=1-informaldummy
tab apq if inlist(careneed,3), sum(formalcare)

xtile PI3=avinc412, nq(3)

gen kidwork=(kidworkFT==1 | kidworkPT==1) if kidworkFT!=.
tab sick if inlist(apq,1,2), sum(kidwork)
tab sick if inlist(apq,4,5), sum(kidwork)
tab sick ltci, sum(kidwork)
tab sick ltci, sum(informaldummy)

tab cohort wave if passets!=.
keep if cohort>=1 & cohort<=5 // dropping cohort 6 (not enough people survives)

gen 	agecohort=67+(wave-4)*2 if cohort==1
replace agecohort=73+(wave-4)*2 if cohort==2 // 72 but can't have even ages in biennial model
replace agecohort=77+(wave-4)*2 if cohort==3
replace agecohort=83+(wave-4)*2 if cohort==4 // 82 but can't have even ages in biennial model
replace agecohort=87+(wave-4)*2 if cohort==5

*to match FT work rates by cohort
tab cohort, sum(kidworkFT)

*turn age into a bi-yearly thing
gen agebi=2*ceil(age/2)

tab agecohort, gen(agecdum)

foreach j in p k {
	foreach c in 1 2 3 4 5 {	
		foreach pct in 10 25 50 75 90 {
			matrix  b`j'`c'_`pct'=J(17,1,.)
			matrix  w`j'`c'_`pct'=J(17,1,.)
			
			forval age=1/17 {
				disp "`j'" `c' `pct' `age'
				cap bootstrap r(p`pct'), reps(1000) seed(31823): sum `j'assets if agecdum`age'==1 & cohort==`c', det
				cap mat b`j'`c'_`pct'[`age',1]=_b[_bs_1]
				cap mat w`j'`c'_`pct'[`age',1]=(_se[_bs_1])^(-2)
			}
			
			svmat b`j'`c'_`pct'
			svmat w`j'`c'_`pct'
			
			replace w`j'`c'_`pct'=0 if w`j'`c'_`pct'==. & b`j'`c'_`pct'!=.
						
		}
	}
}

*ASSET DATA MOMENTS
preserve
keep if _n<=17
forval c=1/5 {
	outsheet bp`c'_251 wp`c'_251 bp`c'_501 wp`c'_501 bp`c'_751 wp`c'_751 using "parent_asset_cohort`c'_moments.out", replace nonames
	outsheet bk`c'_251 wk`c'_251 bk`c'_501 wk`c'_501 bk`c'_751 wk`c'_751 using "child_asset_cohort`c'_moments.out", replace nonames
}
restore	

*EXTRA (ADDING P10 and P90)
preserve
keep if _n<=17
forval c=1/5 {
	outsheet bp`c'_101 wp`c'_101 bp`c'_251 wp`c'_251 bp`c'_501 wp`c'_501 bp`c'_751 wp`c'_751 bp`c'_901 wp`c'_901 using "parent_asset_cohort`c'_moments_extra.out", replace nonames
	outsheet bk`c'_101 wk`c'_101 bk`c'_251 wk`c'_251 bk`c'_501 wk`c'_501 bk`c'_751 wk`c'_751 bk`c'_901 wk`c'_901 using "child_asset_cohort`c'_moments_extra.out", replace nonames
}
restore	

***************************************************************************************************************
